﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform target;//目标
    private float rotateSpeed = 100;//鼠标移动速度
    private Vector3 offsetPosition;//目标与相机偏移量
    private float distance;//相机与目标距离
    private float scrollSpeed = 10;//鼠标滚轮速度

    private RaycastHit hit;
    private bool isSetDistance = false;

    // Use this for initialization
    void Start()
    {
        target = GameObject.FindGameObjectWithTag("Player").transform;//指定跟随的目标是标签为Player的
        offsetPosition = transform.position - target.position;
        //Cursor.lockState = CursorLockMode.Locked;//锁定鼠标在屏幕中心，按Esc键显示鼠标
    }

    // Update is called once per frame
    void Update()
    {
        if (target != null)
        {
            //摄像机跟随player与player保持相对位置偏移 
            transform.position = offsetPosition + target.position;
            RotateView();
            Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, transform.position, Time.deltaTime * 6f);//相机缓动跟随
            Camera.main.transform.LookAt(target);
        }
    }
    void ScrollView(float ScrollWheel)
    {
        //返回位置偏移的向量长度
        distance = offsetPosition.magnitude;

        //根据鼠标滚轮的前后移动获取变化长度
        distance -= ScrollWheel * scrollSpeed;

        //限制变化长度的范围在最小为4最大为22之间
        distance = Mathf.Clamp(distance, 4, 15);
        //新的偏移值为偏移值的单位向量*变换长度
        offsetPosition = offsetPosition.normalized * distance;
    }

    void RotateView()
    {
        //获取摄像机初始位置
        Vector3 pos = transform.position;
        //获取摄像机初始角度
        Quaternion rot = transform.rotation;

        //摄像机围绕player的位置延player的Y轴旋转,旋转的速度为鼠标水平滑动的速度
        transform.RotateAround(target.position, target.up, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime);

        //摄像机围绕player的位置延自身的X轴旋转,旋转的速度为鼠标垂直滑动的速度
        transform.RotateAround(target.position, target.right, Input.GetAxis("Mouse Y") * -rotateSpeed * Time.deltaTime);

        //获取摄像机x轴向的欧拉角
        Vector3 temVec =target.transform.position - transform.position;
        float tempAngle = Mathf.Acos(Vector3.Dot(target.transform.up.normalized, temVec.normalized)) * Mathf.Rad2Deg;//计算两个向量间的夹角
        Debug.Log(tempAngle);
        //如果摄像机的x轴旋转角度超出范围,恢复初始位置和角度
        if (tempAngle < 90 || tempAngle > 160)
        {
            transform.position = pos;
            transform.rotation = rot;
        }

        if (Physics.Linecast(Camera.main.transform.position, target.position, out hit))
        {
            if (hit.transform.tag != "Player")
            {
                //this.transform.position = hit.transform.position;
                offsetPosition = (hit.transform.position - target.position).normalized * Vector3.Distance(hit.transform.position, target.position);
            }
            else
            {
                //更新摄像机与player的位置偏移
                offsetPosition = transform.position - target.position;
            }
        }
        if (isSetDistance == false)
        {
            distance = offsetPosition.magnitude;//仅设置一次
            isSetDistance = true;
        }
        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            ScrollView(Input.GetAxis("Mouse ScrollWheel"));
        }

    }
    private void OnTriggerExit(Collider other)
    {
        if (other.transform.tag == "Wall")
        {
            offsetPosition = offsetPosition.normalized * distance;
        }
    }
}
